<?php
namespace AdminApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class NotificationsApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
		
    }
    public function get($mOrderId)
    {	
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$allUserOrders = $userMedicineOrderTable->getSentQuatationOrders($mOrderId)->toArray();
		//echo '<pre>'; print_r($allUserOrders); exit;
		if(count($allUserOrders)!=0){
			return new JsonModel(array(
				'value'			=>'1',
				'orders'		=> json_encode($allUserOrders),
			));
		}else{
			return new JsonModel(array(
				'value'			=>'0',
				'orders'		=> 	'Orders list is empty',
			));
		}
    }
    public function create($order)
    {
		header('Access-Control-Allow-Origin: *');
		$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$getOrderDetailsd = $orderDeliveryTable->deletePrevious($order['user_medicine_order_id']);
		$getOrderDetails = $orderDeliveryTable->assignOrdersToShops($order);
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$changeStatus = $userMedicineOrderTable->changeStatusOrder($order['user_medicine_order_id']);
		
		$discountTable=$this->getServiceLocator()->get('Models\Model\DiscountsFactory');
		$getOrderDetailsdd = $discountTable->deleteDiscounts($order['user_medicine_order_id']);		
		$saveDiscounts = $discountTable->saveDiscounts($order['user_medicine_order_id'],$order['discount']);
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$getAllUserNotifications = $notificationTable->sendNotificationsToShops($order);
		if(count($getOrderDetails)!=0){
		return new JsonModel(array(
			'shop_order_ids'	=>	json_encode($getOrderDetails),
			'value'		=> 	1,
		));
		}else{
			return new JsonModel(array(
				'status'		=> 'not success',
				'value'		=> 	0,
			));
		}
    }
    public function update($orderId,$order)
    {
		header('Access-Control-Allow-Origin: *');
		$deleteIds="";
		$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		if(isset($order['admin_confirm']) && $order['admin_confirm']=='1')
		{ 
			$result = $orderDeliveryTable->updateShopOrdersByAdmin($order['confirmOrderId'],'confirm');
			if($order['deleteOrderids']!=$order['confirmOrderId']){
				if (strpos($order['deleteOrderids'],$order['confirmOrderId'].'***') !== false) {
					$deleteIds=str_replace($order['confirmOrderId'].'***',"",$order['deleteOrderids']);
				}else{
					$deleteIds=str_replace('***'.$order['confirmOrderId'],"",$order['deleteOrderids']);
				}
				if($order['confirmOrderId']!=$deleteIds){
					$result = $orderDeliveryTable->updateShopOrdersByAdmin($deleteIds,'delete');
				}
			}
			if(count($result)!=0){
				$getOrderdetailes= $orderDeliveryTable->getOrderdetailes($order)->toArray();				
				$sendConfirmation=$notificationTable->sendshopConfirmationorder($getOrderdetailes,$order,'1','3');
				$sendConfirmation=$notificationTable->senduserConfirmationorder($getOrderdetailes,$order,'1','2');
				return new JsonModel(array(
					'value'			=>	1,
				));
			
			}else{
				return new JsonModel(array(
					'value'			=>	0,
				));
			}
		}else{
			$result = $orderDeliveryTable->updateOderWithQutation($orderId,$order);
			if($result!=0){
					return new JsonModel(array(
						'status'			=>	$result,
					));
				
			}else{
				return new JsonModel(array(
					'status'			=>	'Fail',
				));
			}
		}
    }
    public function delete($id)
    {
       
    }
}